package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.OmcWorkLogDto;
import com.csun.cmny.provider.model.domain.OmcWorkLog;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcWorkLogService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-08 14:14
 * @Description: the class omc work log controller
 */
@RestController
@RequestMapping(value = "/workLog", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcWorkLogController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcWorkLogService omcWorkLogService;

    /**
     * 根据日期查询工作日志
     *
     * @param date  日期
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/showByDate/{date}")
    public Wrapper showByDate(@PathVariable String date) {

        LoginAuthDto loginAuthDto = getLoginAuthDto();

        OmcWorkLog omcWorkLog = new OmcWorkLog();
        omcWorkLog.setDate(date);
        omcWorkLog.setUserId(loginAuthDto.getUserId());

        omcWorkLog = omcWorkLogService.selectOne(omcWorkLog);

        //return WrapMapper.ok(omcWorkLog);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),omcWorkLog);
    }

    /**
     * 保存工作日志
     *
     * @param omcWorkLogDto 工作日志
     * @param br            校验结果
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @PostMapping(value = "/save")
    public Wrapper save(@Validated @RequestBody OmcWorkLogDto omcWorkLogDto, BindingResult br) {

        LoginAuthDto loginAuthDto = getLoginAuthDto();

        Integer result = omcWorkLogService.saveWorkLog(omcWorkLogDto, loginAuthDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

}
